package com.campus.controller;

import com.campus.constant.PasswordConstant;
import com.campus.constant.RoleConstant;
import com.campus.entry.ManagerStudent;
import com.campus.pojo.dto.RegisterDto;
import com.campus.pojo.dto.StudentDto;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.AccountService;
import com.campus.service.ManagerStudentService;
import com.campus.service.SysUserService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.Role;
import java.util.List;

@RestController
@RequestMapping("/student")
public class ManagerStudentController {
    @Autowired
    private ManagerStudentService managerStudentService;
    @Autowired
    private AccountService accountService;

    /**
     * 添加学生信息
     * @param managerStudent
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PostMapping("/save")
    public ResponseResult<Object> saveStudent(@RequestBody ManagerStudent managerStudent){
            managerStudentService.addStudent(managerStudent);
            return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 批量导入学生
     * @param managerStudentList
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PostMapping("/saveLists")
    public ResponseResult<Object> saveListStudets(@RequestBody List<ManagerStudent>managerStudentList){
            managerStudentService.addStudentList(managerStudentList);
            return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 查看学生列表
     * @return {@link ResponseResult }<{@link List }<{@link ManagerStudent }>>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/listStudent")
    public ResponseResult<List<ManagerStudent>> listStudent(){
        List<ManagerStudent> managerStudents = managerStudentService.lisStudents();
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,managerStudents);
    }

    /**
     * 审核学生
     * @param id
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PutMapping("/status")
    public ResponseResult<Object> updateStatus(@RequestParam Integer id){
        managerStudentService.updateSatus(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 根据学生Id查询学生
     * @param id
     * @return {@link ResponseResult }<{@link ManagerStudent }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/{id}")
    public ResponseResult<ManagerStudent> getStudentById(@PathVariable Integer id){
        ManagerStudent studentById = managerStudentService.getStudentById(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,studentById);
    }

    /**
     * 根据学生Id更新学生
     * @param id
     * @param managerStudent
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PutMapping("/updateStudent/{id}")
    public ResponseResult<Object> updateStudentById(@PathVariable Integer id,@RequestBody ManagerStudent managerStudent){
        managerStudent.setId(id);
        managerStudentService.updateStudent(managerStudent);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 模糊查询学生
     * @param stuName
     * @param grade
     * @param isFirst
     * @param status
     * @return {@link ResponseResult }<{@link List }<{@link ManagerStudent }>>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/listStudentWithFilter")
    public ResponseResult<List<ManagerStudent>> listStudentWithFilter(
            @RequestParam(required = false) String stuName,
            @RequestParam(required = false) String grade,
            @RequestParam(required = false) String isFirst,
            @RequestParam(required = false) Integer status) {

        List<ManagerStudent> managerStudents = managerStudentService.listStudentsWithFilter(stuName, grade, isFirst, status);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, managerStudents);
    }

    /**
     * 根据学生id删除学生
     * @param id
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @DeleteMapping("/{id}")
    public ResponseResult<Object> removeStudentById(@PathVariable Integer id){
        managerStudentService.removeStudentById(id);
        return new  ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 获取学生列表
     * @return {@link ResponseResult }<{@link List }<{@link ManagerStudent }>>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/getStudent")
    public ResponseResult<List<ManagerStudent>> getStudent(){
        List<ManagerStudent> list=managerStudentService.getStudent();
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,list);
    }




}
